package com.pac.register.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.common.tools.StringTools;
import com.common.tools.TimeTools;
import com.common.util.LogicException;
import com.common.util.MsgKeyContent;
import com.common.util.PropertyUtils;
import com.common.util.SessionKeyContent;
import com.pac.doctor.bean.DoctorBean;
import com.pac.file.bean.FileBean;
import com.pac.file.service.FileService;
import com.pac.role.RoleKeyContent;
import com.pac.user.bean.UserBean;
import com.pac.user.service.UserService;

/**
 * 用户注册控制类
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月15日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@Scope("prototype")
@RequestMapping("/register")
public class RegisterController {

	private static Logger logger = Logger.getLogger(RegisterController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private FileService fileService;

	/**
	 * 用户注册
	 * 
	 * @param userBean
	 *            用户信息
	 * @param roleId
	 *            角色ID
	 * @param imgCode
	 *            图片验证码
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/regist")
	@ResponseBody
	public Map<String, Object> regist(HttpServletRequest request,
			UserBean userBean, DoctorBean doctorBean, String roleId,
			String imgCode, MultipartFile headerImg) {

		Map<String, Object> map = new HashMap<String, Object>();

		// 检查验证码
		HttpSession session = request.getSession();
		String signcodeSession = (String) session
				.getAttribute(SessionKeyContent.SESSION_KEY_STR_LOGIN_SIGN_CODE);

		// 验证的时候不区分大小写
		imgCode = imgCode.toUpperCase();
		signcodeSession = signcodeSession.toUpperCase();
		if (!imgCode.equals(signcodeSession)) {
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT, false);
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT_MSG, "验证错误");
			return map;
		}

		try {

			if (RoleKeyContent.ROLE_ID_GENERAL.equals(roleId)) {// 普通用户注册
				userService.register(userBean, roleId);
			} else if (RoleKeyContent.ROLE_ID_DOCTOR.equals(roleId)) {// 医生注册

				String headImg = "";

				if (headerImg.getSize() > 0) {
					String filePath = "doctor/" + TimeTools.now("yyyy") + "/"
							+ TimeTools.now("MM") + "/" + TimeTools.now("dd");
					String tempDir = PropertyUtils.getValue("file.temp.dir");
					tempDir += "/" + StringTools.randomCode(32) + ".tmp";
					File upFile = new File(tempDir);
					try {
						headerImg.transferTo(upFile);
					} catch (IllegalStateException e1) {
						logger.error(e1);
					} catch (IOException e1) {
						logger.error(e1);
					}

					FileBean fileBean = fileService.uploadImg(upFile,
							headerImg.getOriginalFilename(), filePath, true,
							200, 266);

					headImg = fileBean.getPath();
				}

				userBean.setHeadImg(headImg);
				doctorBean.setImg(headImg);
				userService.doctorRegister(userBean, roleId, doctorBean);
			}

			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT, true);
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT_MSG, "注册成功");
		} catch (LogicException ex) {
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT, false);
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT_MSG,
					ex.getMessage());
		} catch (Exception ex) {
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT, false);
			map.put(MsgKeyContent.PAGE_AJAX_OPERATION_RESULT_MSG, "系统内部错误");
		}

		return map;
	}

	@RequestMapping("/gotoPage")
	public ModelAndView gotoPage(String pageName) {
		ModelAndView mov = new ModelAndView();
		mov.setViewName("/register/" + pageName);
		return mov;
	}

}
